package cn.edu.hqu.exp.courseselection.util;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import cn.edu.hqu.exp.courseselection.domain.Course;
import cn.edu.hqu.exp.courseselection.repository.CourseRepository;


@Component
public class CourseByIdConverter implements Converter<String,Course>{
	private CourseRepository courseRepo;
	
	@Autowired
	public CourseByIdConverter(CourseRepository courseRepo) {
		this.courseRepo = courseRepo;
	}
	
	@Override
	public Course convert(String id) {
		Optional<Course> optionalCourse = courseRepo.findById(id);
		return optionalCourse.isPresent() ?
				optionalCourse.get() : null;
	}
}
